package com.calsoft.Hops_Mobile_Tablet.base;
/*
 * User: Minh Diep
 * Date: 7/8/13
 * Time: 2:55 PM
 */

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.calsoft.Hops_Mobile_Tablet.R;

import java.util.ArrayList;

public class OpToursItemAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private ArrayList<OpToursItemData> mData = new ArrayList<OpToursItemData>();
    private Context context;


    public OpToursItemAdapter(Context context){
        this.context = context;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void addItem(OpToursItemData item){
        mData.add(item);
        notifyDataSetChanged();
    }

    public void removeItem(int position){
        mData.remove(position);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public OpToursItemData getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public static class ViewHolder {
        public TextView titleTextView;
        public TextView labelTextView;
        public TextView priceTextView;
        public TextView locationTextView;
        public ImageView imageView;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        int type = getItemViewType(position);
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.card_ui_item, null);
            holder.titleTextView = (TextView) convertView.findViewById(R.id.service_title);
            holder.locationTextView = (TextView) convertView.findViewById(R.id.service_location);
            holder.labelTextView = (TextView) convertView.findViewById(R.id.op_tours_service_type);
            holder.priceTextView = (TextView) convertView.findViewById(R.id.service_price);
            holder.imageView = (ImageView) convertView.findViewById(R.id.iv_service);
            assert convertView != null;
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        holder.titleTextView.setText(mData.get(position).getDisplayName());
        holder.priceTextView.setText("From $" + mData.get(position).getPrice());
        holder.labelTextView.setText(mData.get(position).getServiceType());

        if (mData.get(position).getCity().equals("CNS")){
            holder.locationTextView.setText("Location: Cairns");
        } else {
            holder.locationTextView.setText("Location: Gold Coast");
        }


        String imageName = mData.get(position).getImageTag();
        int drawableImageId = context.getResources().getIdentifier(imageName, "drawable", context.getPackageName());
        holder.imageView.setImageResource(drawableImageId);

        return convertView;
    }
}
